package cn.org.xwb.bookmall.admin.aspect;

import cn.org.xwb.bookmall.admin.service.LoginService;
import cn.org.xwb.bookmall.model.constant.AccountConst;
import cn.org.xwb.common.model.vo.RespResult;
import cn.org.xwb.common.utils.AspectUtil;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Order(20)
@Component
@Log4j2
public class JudgeIsLoginAspectParam {

    @Autowired
    private LoginService loginService;

    @Pointcut("@annotation(cn.org.xwb.bookmall.admin.annotation.IsLoginParam)")
    private void pointcut() {
    }

    /**
     * 判断是否登录
     *
     * @param proceedingJoinPoint 切点
     * @return 切点方法执行返回结果
     */
    @Around("pointcut()")
    public Object judgeIsLoginAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 获取 account 的值
        String account = (String) AspectUtil.getPointMethodParamValByName(proceedingJoinPoint, AccountConst.ACCOUNT_PARAM_NAME);
        // 判断登录是否有效
        RespResult<Object> respResult = loginService.effective(account);
        // 登录无效
        if (!respResult.getStatus()) return respResult;
        // 登录有效，可以执行
        return proceedingJoinPoint.proceed();
    }

}
